<?php

class WSB_Controller_Url
{

	/**
	 * Erstellt eine URL.
	 *
	 * @param string $controllerName Der Controllername
	 * @param string $actionName Der Actionname
	 * @param array $parameters Die Parameter welche dem Controller uebergeben werden
	 * @param string $moduleName Der Modulname
	 * @param array $globalParameters Die globalen Parameter welche dem System zur Verfuegung stehen
	 * @return string The parsed Url as string.
	 */
	public static function getUrl($controllerName, $actionName, $parameters=array(), $moduleName=null, $globalParameters=array())
	{
		$url = WSB_IS_ADMIN_MODE ? '' : 'admin/';
		if(!empty($moduleName)) {
			$url .= $moduleName . '/';
		}
		if(!empty($controllerName)) {
			$url .= $controllerName . '/';
		}
		if(!empty($actionName)) {
			$url .= $actionName . '/';
		}
		if(count($parameters) > 0) {
			$str = '';
			foreach($parameters AS $key=>$val) {
				$url .= $key . '/' . $val . '/';
			}
		}

		$linkVars = array('TEMPLATE');
		foreach($linkVars AS $linkVar) {
            if(isset($_GET[$linkVar])) {
            	if(!array_key_exists($linkVar, $globalParameters)) {
					$globalParameters[$linkVar] = $_GET[$linkVar];
            	}
            }
		}

		// Parameter ohne Wert
		// (Bsp. Parameter reset in http://localhost/wsb/admin/module/controller/action/?reset)
		foreach($globalParameters AS $key=>$val) {
			if(is_null($val)) {
				$globalParameters[$key] = 1;
			}
		}

		$query = urldecode(http_build_query($globalParameters));
		if(!empty($query)) {
			$query = '?' . $query;
		}

		return $url . $query ;
	}

	public static function getHtmlLink($label, $href, $attributes=array())
	{
	    if(!is_array($attributes)) {
            throw new Zend_Exception('Error: third paramter has to be an array');
	    }
	    $sAttributes = '';
	    if(count($attributes) > 0) {
            foreach($attributes AS $key=>$value) {
                $sAttributes .= sprintf(' %s="%s"', $key, $value);
            }
	    }
	    return sprintf('<a href="%s"%s>%s</a>', $href, $sAttributes, $label);
	}

	public static function getHtmlPopupLink($label, $href, $width=null, $height=null, $attributes=array())
	{
		$config = Zend_Registry::get('Zend_Config');
	    $width = empty($width) ? $config->application->defaultPopupWidth : intval($width);
	    $height = empty($height) ? $config->application->defaultPopupHeight : intval($height);
        if(array_key_exists('onclick', $attributes)) {
            throw new Zend_Exception('Error: key onclick is not allowed in.');
        } else {
            $attributes['onclick'] = sprintf('javascript:createPopup(%d, %d, this);return false;', $width, $height);
        }
        $script = '
	        function createPopup(width, height, doc)
	{
	        	popup=window.open(doc.href, doc.target, \'height=\'+height+\',width=\'+width+\',status,resizable=yes,scrollbars=yes\');
            	popup.focus();
            	return false;
            }
        ';
		static $set = false;
	    if(!$set) {
		    if(WSB_IS_ADMIN_MODE) {
	    		$view = Zend_Layout::getMvcInstance()->getView();
	    	} else {
		    	$view = WSB_Frontend::getInstance()->getView();
	    	}
			$view->headScript()->prependScript($script);
			$set = true;
	    }
        return self::getHtmlLink($label, $href, $attributes);
	}

}
